<template>
  <div class='home-wrapper' v-loading="homeState.loading">
    <HpRender v-if="homeState.homeCode" :homeCode="homeState.homeCode"/>
  </div>
</template>

<script setup>
import { HpRender } from '@hp'
import { defAxios } from '@/utils/http'
import { nextTick, onBeforeMount, reactive } from 'vue';

// 主页数据集
const homeState = reactive({
  loading: false,
  homeCode: ''
})

/**
 * 加载主页信息
 */
const loadHomeData = async () => {
  if (homeState.loading) return
  homeState.loading = true
  const homeData = await reqGetHomeInfo()
  if (!homeData) {
    homeState.loading = false
    return
  }
  homeState.homeCode = homeData.homeCode
  nextTick(() => { homeState.loading = false })
}

/**
 * 请求获取当前用户的主页配置信息
 */
const reqGetHomeInfo = () => {
  return new Promise(resolve => {
    defAxios({
      url: '/userRoleHome/getUserHome',
      method: 'POST'
    }).then(res => {
      if (res.code === 200) {
        resolve(res.data)
      } else {
        console.error(res.msg)
        resolve(null)
      }
    }).catch(err => {
      console.error(err)
      resolve(null)
    })
  })
}

onBeforeMount(() => {
  loadHomeData()
})


</script>

<style lang="scss" scoped>
</style>